<template>
  <div class="app-container customer-div">
    <div>
      <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="changeTab">
        <el-menu-item index="my">我的</el-menu-item>
        <el-menu-item index="all" v-if="deliverDetailsAll">全部</el-menu-item>
      </el-menu>
    </div>
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="发货单号：">
          <el-input v-model="listQuery.deliverSn"></el-input>
        </el-form-item>
        <el-form-item label="发货日期：">
          <el-date-picker v-model="listQuery.deliverDate" type="daterange" format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单号：">
          <el-input v-model="listQuery.orderSn"></el-input>
        </el-form-item>
        <el-form-item label="客户编号：">
          <el-input v-model="listQuery.customerNumber"></el-input>
          <!--          <el-autocomplete-->
          <!--                  clearable-->
          <!--                  class="inline-input"-->
          <!--                  value-key="customerName"-->
          <!--                  :fetch-suggestions="querySearch"-->
          <!--                  v-model="listQuery.customerName"-->
          <!--                  placeholder="请输入内容"-->
          <!--                  @select="handleSelectCustomerName"-->
          <!--                  :trigger-on-focus="false"-->
          <!--          ></el-autocomplete>-->
        </el-form-item>
        <el-form-item label="业务员：">
          <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable>
            <el-option v-for="item in userOptionList" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="送货类型：">
          <el-select v-model="listQuery.isYunlianDeliver" filterable placeholder="请选择" clearable>
            <el-option :key="1" label="运联发货" :value="1">
            </el-option>
            <el-option :key="0" label="采购发货" :value="0">
            </el-option>
            <el-option :key="2" label="自提" :value="2">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用类型：">
          <el-select v-model="listQuery.freightType" filterable placeholder="请选择" clearable>
            <el-option key="sale" label="销售承担" value="sale"></el-option>
            <el-option key="purchase" label="采购承担" value="purchase"></el-option>
            <el-option key="logistics" label="运联承担" value="logistics"></el-option>
            <el-option key="customer" label="客户承担" value="customer"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="牌号：">
          <el-select v-model="listQuery.kingGoodsId" filterable placeholder="请选择" clearable>
            <el-option v-for="item in goodsList" :key="item.kingId" :label="item.goodsSn" :value="item.kingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单类型：">
          <el-select v-model="listQuery.isMoresuOnlineOrder" class="input-width" style="width:150px" placeholder="请选择"
            clearable>
            <el-option :key="0" label="销售订单" :value="0"></el-option>
            <el-option :key="1" label="推广订单" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货状态：">
          <el-select v-model="listQuery.goodsReceiptState" class="input-width" style="width:150px" placeholder="请选择"
            clearable>
            <el-option :key="0" label="未收货" :value="0"></el-option>
            <el-option :key="1" label="已收货" :value="1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否确认函：">
          <el-select v-model="listQuery.confirmationLetter" filterable placeholder="请选择" clearable style="width:100px">
            <el-option :key="1" label="是" :value="1"></el-option>
            <el-option :key="0" label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="车牌号：">
          <el-input v-model="listQuery.driverCardNo"></el-input>
        </el-form-item>

        <el-form-item label="物流毛利归属人：" label-width="140px" v-if="listQuery.tab == 'all'">
          <el-select v-model="listQuery.freightUserId" filterable placeholder="请选择" clearable>
            <el-option v-for="item in userOptionList2" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="派单员：">
          <el-select v-model="listQuery.driverChargeUserId" filterable placeholder="请选择" clearable>
            <el-option v-for="item in userOptionList" :key="item.userId" :label="item.userName" :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否整车：" >
          <el-select v-model="listQuery.isCompleteCar" placeholder="请选择" clearable>
            <el-option :key="1" label="整车" value="整车"></el-option>
            <el-option :key="0" label="散货" value="散货"></el-option>
            <el-option :key="2" label="拼车" value="拼车"></el-option>
            <el-option :key="3" label="专线" value="专线"></el-option>
            <el-option :key="4" label="铁路" value="铁路"></el-option>
            <el-option :key="5" label="海运" value="海运"></el-option>
            <el-option :key="6" label="快运" value="快运"></el-option>
            <el-option :key="7" label="快递" value="快递"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="物流毛利：" >
          <el-select v-model="listQuery.freightProfitType" placeholder="请选择" clearable>
            <el-option :key="1" label="正" value="1"></el-option>
            <el-option :key="0" label="负" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleSearchList()" type="primary">
            查询
          </el-button>
          <el-button v-if="deliverDetailsExport" @click="downloadExcel" type="primary">
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <div style="font-size:14px;line-height: 30px;padding:10px 0 ">
        物流总毛利：{{ allLogisticsProfit }}
        <span style="padding-left: 20px">发货数量：{{sendAllNum}}</span>
        <span style="padding-left: 20px">运联含税运费：{{ogFreight}}</span>
        <span style="padding-left: 20px">实际运费：{{actualFreight}}</span>
      </div>
      <el-table v-loading="listLoading" :data="tableData" border style="width: 100%; margin-top: 20px">
        <el-table-column label="单据类型" width="100" align="center" prop="deliverType">
        </el-table-column>
        <el-table-column label="发货日期" width="100" align="center" prop="shipTime">
        </el-table-column>
        <el-table-column label="发货单号" width="150" align="center">
          <template slot-scope="scope">
            <span style="vertical-align: middle;cursor: pointer;color:#409EFF"
              @click="toDetail(scope.row)">{{ scope.row.deliverSn }}</span>
            <img src="../../assets/images/zhuofei2.png" style="vertical-align: middle;" v-if="scope.row.isDelete" />
          </template>
        </el-table-column>
        <el-table-column label="订单日期" width="100" align="center" prop="orderDate">
        </el-table-column>
        <el-table-column label="订单号" width="150" align="center">
          <template slot-scope="scope">
            <span style="font-size:12px;vertical-align: middle">{{ scope.row.orderSn }}</span>
            <img src="../../assets/images/xianshang.png" v-if="scope.row.isMoresuOnlineOrder == 1"
              style="vertical-align: middle;" />
          </template>
        </el-table-column>
        <el-table-column label="送货类型" align="center" width="100">
          <template slot-scope="scope">
            {{ scope.row.sendType }}
          </template>
        </el-table-column>
        <el-table-column label="业务员" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.userRealName }}
          </template>
        </el-table-column>
        <el-table-column label="客户编号" width="300">
          <template slot-scope="scope">
            <span>
              {{ scope.row.customerNumber }}
            </span>
            <!--是否有权限-->
            <div v-if="scope.row.isShowCustomerName">{{ scope.row.customerName }}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="司机" width="100">
          <template slot-scope="scope">
            {{ scope.row.driverName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="手机号" width="100">
          <template slot-scope="scope">
            {{ scope.row.driverPhone }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="车牌号" width="100">
          <template slot-scope="scope">
            {{ scope.row.driverCardNo }}
          </template>
        </el-table-column>
        <el-table-column label="品名" align="center" width="150">
          <template slot-scope="scope">
            {{ scope.row.goodsName }}
          </template>
        </el-table-column>
        <el-table-column label="牌号" align="center" width="150">
          <template slot-scope="scope">
            {{ scope.row.goodsSn }}
          </template>
        </el-table-column>
        <el-table-column prop="goodsNum" label="订单数量" width="100" align="center">
        </el-table-column>
        <el-table-column label="订单行备注" width="400">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column label="本次发货数量" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.shipNum }}
          </template>
        </el-table-column>
        <el-table-column label="发货批号" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.batchNo }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="销售合同结算方式" width="150">
          <template slot-scope="scope">
            {{ scope.row.salePayMethod }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="成本价结算方式" width="150">
          <template slot-scope="scope">
            {{ scope.row.goodsPayMethod }}
          </template>
        </el-table-column>
        <el-table-column label="运联含税运费" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.ogFreight }}
          </template>
        </el-table-column>
        <el-table-column label="实际运费" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.actualFreight }}
          </template>
        </el-table-column>
        <el-table-column label="运费差异" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.diffFreight }}
          </template>
        </el-table-column>
        <el-table-column label="运费差异承担" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.diffFreightType }}
          </template>
        </el-table-column>
        <el-table-column label="司机运费" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.driverExpress }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="费用类型" width="100">
          <template slot-scope="scope">
            {{ scope.row.freightType }}
          </template>
        </el-table-column>
        <el-table-column label="预计发货仓库" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.warehouseName }}
          </template>
        </el-table-column>
        <el-table-column label="实际发货仓库" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.shipWarehouse }}
          </template>
        </el-table-column>
        <el-table-column label="物流延迟金" width="120" align="center" prop="deliverInterest">
        </el-table-column>
        <el-table-column label="物流毛利" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.logisticsProfit }}
          </template>
        </el-table-column>
        <el-table-column label="毛利归属" width="100" align="center">
          <template slot-scope="scope">
             {{scope.row.freightUserName}}
          </template>
        </el-table-column>
        <el-table-column label="期望发货日期" width="120" align="center">
          <template slot-scope="scope">
            <span style="vertical-align: middle">{{ scope.row.expectSendDate }}</span><img
              v-if="scope.row.urgentTypeIcon != ''" :src="scope.row.urgentTypeIcon"
              style="width:14px;vertical-align: middle;margin-left:5px" />
          </template>
        </el-table-column>
        <el-table-column label="发货紧急度" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.urgentTypeStr }}
          </template>
        </el-table-column>
        <el-table-column label="预计发货日期" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.sendDate }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="发货日期" width="100">
          <template slot-scope="scope">
            {{ scope.row.shipDate }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="收货状态" width="100">
          <template slot-scope="scope">
            {{ scope.row.goodsReceiptState == 1 ? '已收货' : '未收货' }}
          </template>
        </el-table-column>
        <el-table-column label="收货凭证" width="150" align="center">
          <template slot-scope="scope">
            <img :src="item" v-for="(item,index) in scope.row.goodsReceipt" :key="index" @click="fullImg(item)"
              style="margin-left:5px;margin-bottom:5px;cursor: pointer;width:30px;height:30px;object-fit: contain" />
          </template>
        </el-table-column>

        <el-table-column show-overflow-tooltip label="收货确认函" width="120" align="center">
          <template slot-scope="scope">
            <div @click="fullImg2(item)" style="color:#409EFF;cursor: pointer;word-break:break-all"
              v-for="(item,index) in scope.row.confirmationPic" :key="index">查看附件</div>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize" :page-sizes="[10, 15, 20]" :total="count">
      </el-pagination>
    </div>

    <!--放大图片-->
    <el-dialog :visible.sync="dialogVisible5" width="800px">
      <img :src="fullSrc" style="width:100%;" />
    </el-dialog>

  </div>
</template>
<script>
import { getCustomerOption, getOrderStatList, getOrderUsersList, getDeliverDetailList, exportDeliverDetailList, orderGoodsList } from '@/api/partner'
import { formatDate } from '@/utils/date';
import store from '@/store'
import { mapGetters } from 'vuex'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  goodsName: '',
  createDate: null,
  deliverDate: null,
  customerName: null,
  deliverSn: '',
  userId: '',
  isYunlianDeliver: '',
  freightType: '',
  goodsSn: '',
  kingGoodsId: '',
  isMoresuOnlineOrder: '',
  goodsReceiptState: '',
  confirmationLetter: '',
  driverCardNo: '',
  customerNumber: '',
  tab:'my',
  freightUserId:'',
  type:'warehouse',
  driverChargeUserId:'',
  isCompleteCar:'',
  freightProfitType:''
};
export default {
  name: "index",
  data() {
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles: store.getters.roles ? store.getters.roles : [],
      userOptionList: [],
      goodsList: [],
      dialogVisible5: false,
      fullSrc: '',
      allLogisticsProfit: 0,
      userOptionList2:[],
      sendAllNum:0,
      actualFreight:0,
      ogFreight:0
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),

    isCanSaleByDept() {
      if (this.roles.indexOf("saleDept") != -1) {
        return true
      } else {
        return false
      }
    },
    isCanSaleByAll() {
      if (this.roles.indexOf("saleAll") != -1) {
        return true
      } else {
        return false
      }
    },
    deliverDetailsExport() {
      if (this.roles.indexOf("deliverDetailsExport") != -1) {
        return true
      } else {
        return false
      }
    },
    deliverDetailsAll() {
      if (this.roles.indexOf("deliverDetailsAll") != -1) {
        return true
      } else {
        return false
      }
    },
  },
  created() {
    let now = new Date().getTime();
    let endTime = now + 60 * 60 * 24 * 1000 * 2;
    let startTime = endTime - 60 * 60 * 24 * 1000 * 30;
    let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd');
    let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
    this.listQuery.deliverDate = [startDate, endDate];
    this.getList();
    this.getUserOptionList();
    orderGoodsList().then((res) => {
      this.goodsList = res.list
    })
    this.getUserOptionList2()
  },
  filters: {
    formatCreateTime(time) {
      if (time) {
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      } else {
        return '--'
      }
    },
  },
  methods: {
    changeTab(key) {
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery.tab = key
      let now = new Date().getTime();
      let endTime = now + 60 * 60 * 24 * 1000 * 2;
      let startTime = endTime - 60 * 60 * 24 * 1000 * 30;
      let startDate = formatDate(new Date(startTime), 'yyyy-MM-dd');
      let endDate = formatDate(new Date(endTime), 'yyyy-MM-dd');
      this.listQuery.deliverDate = [startDate, endDate];
      this.getList();
    },
    //放大图片
    fullImg2(item) {
      window.open(item)
    },
    fullImg(src) {
      this.dialogVisible5 = true;
      this.fullSrc = src;
    },
    //查看发货单
    toDetail(item) {
      localStorage.setItem('deliverListQuery', JSON.stringify(this.listQuery));
      console.log(item)
      let routeData = this.$router.resolve({ name: 'deliverCon', query: { id: item.deliverId, type: 'edit' } });
      window.open(routeData.href);
    },
    //下载excle
    downloadExcel() {
      let params = JSON.parse(JSON.stringify(this.listQuery));
      params.type = 'warehouse';
      exportDeliverDetailList(params).then((res) => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('sheet/Export2Excel');
          const tHeader = res.title;
          const filterVal = res.columns;
          const list = res.list;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, res.fileName);
        })
      }).catch(() => { })
    },

    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },

    renderHeader(h) {
      return [h('div', {}, ['金蝶']), h('div', {}, ['成本单价'])]
    },

    renderHeader2(h) {
      return [h('div', {}, ['成本单价']), h('div', {}, ['现金价'])]
    },
    renderHeader3(h) {
      return [h('div', {}, ['采购底价']), h('div', {}, ['现金价'])]
    },

    //获取员工下拉选项
    getUserOptionList() {
      getOrderUsersList().then((res) => {
        this.userOptionList = res;
      })
    },

    //获取物流下拉选项
    getUserOptionList2() {
      getOrderUsersList({departmentId:4,status:1}).then((res) => {
        this.userOptionList2 = res;
      })
    },

    //选择员工名称
    handleSelectUserName(key, item) {
      this.listQuery[key] = item.userId;
    },


    //客户名称输入匹配
    querySearch(queryString, cb) {
      getCustomerOption({ keywords: queryString }).then((res) => {
        cb(res);
      })
    },


    //选择客户名称
    handleSelectCustomerName(item) {
      this.listQuery.keywords = item.customerName
    },

    /**
     * 切换选择状态
     */
    handleSelect(key, keyPath) {
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery)),
        this.listQuery.tab = key;
      this.handleSearchList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },

    //获取发货明细列表
    getList() {
      this.listLoading = true;
      getDeliverDetailList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.allLogisticsProfit = response.allLogisticsProfit;
        this.sendAllNum = response.sendAllNum
        this.actualFreight = response.actualFreight
        this.ogFreight = response.ogFreight
        this.listLoading = false;
      }).catch(() => {
        this.listLoading = false;
      });
    },

  },
}
</script>

<style scoped lang="scss">
.upload-div {
  text-align: center;
  position: relative;

  #upload {

    opacity: 0;
    position: absolute;
    z-index: 1;
    width: 154px;
    height: 40px
  }
}
</style>
